﻿// -----------------------------------------------------------------------
// <copyright file="CookieState.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace NetFramework.Web
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NetFramework.Web.Configuration;
    using NetFramework.Web.Resources;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CookieState
    {
        #region [ Get ]
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetValue(string name)
        {
            return CookieManager.GetCookie(name);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValue(string name, string key)
        {
            return CookieManager.GetCookie(name, key);
        }

        #endregion

        #region [ Save ]

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void Save(string name, object value)
        {
            ConfigCookieEntry cookieInfo = GetConfigCookieEntry(name);
            
            TimeSpan expireDate = DateTime.Now.AddMinutes(20).TimeOfDay;
            string domain = string.Empty;
            string path = "/";
            bool requireSSL = false;
            bool httpOnly = false;
            if (cookieInfo != null)
            {
                expireDate = cookieInfo.ExpiresAfter;
                domain = GetCookieDomain(cookieInfo.TopLevelDomain);
                path = cookieInfo.Path;
                requireSSL = cookieInfo.SecureOnly;
                httpOnly = cookieInfo.RenderInClient;
            }
            else
            {
                throw new Exception(string.Format("cookie {0} not found in config file", name));
            }

            CookieManager.SetCookie(name, Convert.ToString(value), domain, path, httpOnly, requireSSL, expireDate);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void Save(string name, string key, object value)
        {
            ConfigCookieEntry cookieInfo = GetConfigCookieEntry(name);

            TimeSpan expireDate = DateTime.Now.AddMinutes(20).TimeOfDay;
            string domain = string.Empty;
            string path = "/";
            bool requireSSL = false;
            bool httpOnly = false;
            if (cookieInfo != null)
            {
                expireDate = cookieInfo.ExpiresAfter;
                domain = GetCookieDomain(cookieInfo.TopLevelDomain);
                path = cookieInfo.Path;
                requireSSL = cookieInfo.SecureOnly;
                httpOnly = cookieInfo.RenderInClient;
            }
            else
            {
                throw new Exception(string.Format("cookie {0} not found in config file", name));
            }

            CookieManager.SetCookie(name, key, Convert.ToString(value), domain, path, httpOnly, requireSSL, expireDate);
        }
        #endregion

        #region [ Remove ]
        /// <summary>
        /// 移除某个子键。
        /// </summary>
        /// <param name="name"></param>
        /// <param name="key"></param>
        public static void Remove(string name, string key)
        {
            string domain = GetCookieDomain(name);
            CookieManager.ClearCookie(name, key, domain);
        }

        /// <summary>
        /// 移除一个状态。
        /// </summary>
        /// <param name="name"></param>
        public static void Remove(string name)
        {
            string domain = GetCookieDomain(name);
            CookieManager.ClearCookie(name, domain);
        }

        /// <summary>
        /// 清除所有的状态。
        /// </summary>
        public static void Clear()
        {
            foreach (ConfigCookieEntry entry in WebResourceConfigHelper.CookieConfig.ConfigCookieEntrys)
            {
                Remove(entry.Name);
            }
        }

        #endregion

        private static ConfigCookieEntry GetConfigCookieEntry(string cookieName)
        {
            return WebResourceConfigHelper.CookieConfig.GetConfigCookieEntry(cookieName);
        }

        /// <summary>
        /// 获取Cookie有效域。
        /// </summary>
        /// <param name="topLevelDomain"></param>
        /// <returns></returns>
        private static string GetCookieDomain(bool topLevelDomain)
        {
            return topLevelDomain ? WebResourceConfigHelper.WebsiteConfig.Domain : "";
        }

        private static string GetCookieDomain(string cookieName)
        {
            ConfigCookieEntry cookieInfo = GetConfigCookieEntry(cookieName);
            bool topLevelDomain = cookieInfo == null ? false : cookieInfo.TopLevelDomain;

            return GetCookieDomain(topLevelDomain);
        }
    }
}
